package io.renren.modules.avanotes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.BizUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.vo.GroupItemVo;
import io.renren.common.vo.output.EchartsGraphHeatmapVo;
import io.renren.modules.avanotes.dao.QuestionBankDetailDao;
import io.renren.modules.avanotes.entity.QuestionBankDetailEntity;
import io.renren.modules.avanotes.service.QuestionBankDetailService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.*;


//@Service("questionBankDetailService")//默认只有一个实现，那就不写名称了直接使用默认的名称！
@Service
public class QuestionBankDetailServiceImpl extends ServiceImpl<QuestionBankDetailDao, QuestionBankDetailEntity> implements QuestionBankDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<QuestionBankDetailEntity> page = this.page(
                new Query<QuestionBankDetailEntity>().getPage(params),
                new QueryWrapper<QuestionBankDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public EchartsGraphHeatmapVo heatmap() {
        EchartsGraphHeatmapVo vo = new EchartsGraphHeatmapVo();
        //1.获取数据
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        //select day,count(day) as num from `question_bank_detail` where create_user_id=1 group by day order by day
        //List<QuestionBankDetailEntity> list = this.list(new QueryWrapper<QuestionBankDetailEntity>().eq(QuestionBankDetailEntity.CN_CREATE_USER_ID, userEntity.getUserId()).groupBy(QuestionBankDetailEntity.CN_DAY).orderBy(true, true, QuestionBankDetailEntity.CN_DAY));
        List<GroupItemVo> itemVos = this.baseMapper.selectListGroupByDay(userEntity.getCreateUserId());
        Map<String, Integer> map = BizUtils.genDateNumMap(itemVos);
        //2.计算时间
        Date now = new Date();

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        //NOTEME 由于月份从0开始，所以需要加1
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
//        calendar.setTime(new Date());
//        calendar.set(Calendar.YEAR, year - 1);
//        Date tempDate = calendar.getTime();

        LocalDate end = LocalDate.of(year, month, day);
        LocalDate begin = end.plusYears(-1);
        List<List<Object>> data = new ArrayList<>();
        for (LocalDate tDate = begin; tDate.compareTo(end) <= 0; tDate =tDate.plusDays(1)) {
            List<Object> d = new ArrayList<>(2);
            //2021-03-01
            String dt = tDate.toString();
            d.add(dt);
            int num = 0;
            if (map.containsKey(dt)) {
                //如果已经存在
                num = map.get(dt);
            }
            d.add(num);
            data.add(d);
        }
        vo.setBegin(begin.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli());
        vo.setEnd(now.getTime());
        vo.setData(data);
        return vo;

//        long beginTs = tempDate.getTime();
//        long endTs = now.getTime();
//        long internal = 3600 * 24 * 1000;
//
//        for (int i = 0; i < endTs; i += internal) {
//            List<Object> d = new ArrayList<>(2);
//        }
    }

}